/*
 * File:   form_equalization.cpp
 * Author: dima
 *
 * Created on 10 января 2015 г., 15:17
 */
#include "ui_form_equalization.h"
#include "form_equalization.h"
#include "ipd_exception.h"
#include "ipd_equalization.h"
#include "ipd_img.h"

#include "widget_histogram.h"
#include "widget_curve.h"
#define step_color 10

#define step_grid_y 0.1//The dimension of the grid in Y
#define start_y 0 //The range of values for Y
#define end_y 1

form_equalization::form_equalization(ipd::ipd_img &img) : widget(new Ui::form_equalization()), original_image(img)
{
    histogram = NULL;
    curve = NULL;
    equalization = new ipd::ipd_equalization(original_image,step_color);
    convertible_image = new ipd::ipd_img();
    widget->setupUi(this);
}

void form_equalization::init()//initialization class
{
    equalization->convert_image(*convertible_image);
}

form_equalization::~form_equalization()
{
    delete_params();
    delete equalization;
    delete convertible_image;
    delete widget;
}

void form_equalization::create_params() //create widget histogram and widget curve
{
    delete_params();
    switch (original_image.get_color_type())
    {
    case ipd::tp_RGB:
    {
        QString x_str = QString::fromUtf8("Histogram value");
        QString y_str[] = {QString::fromUtf8("Value R"), QString::fromUtf8("Value G"), QString::fromUtf8("Value B")};
        histogram = new widget_histogram[sz_RGB];
        curve = new widget_curve[sz_RGB];

        QRect cr_histogram[] = {QRect(0, 0, 1, 1), QRect(0, 1, 1, 1), QRect(0, 2, 1, 1)};
        QRect cr_curve[] = {QRect(1, 0, 1, 1), QRect(1, 1, 1, 1), QRect(1, 2, 1, 1)};
        for (int i = 0; i < sz_RGB; i++)
        {
            histogram[i].set_size_XY(start_y, end_y, step_grid_y, original_image.get_min_color(),original_image.get_max_color(), step_color);
            histogram[i].set_title_coordinates(x_str,y_str[i]);
            histogram[i].setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            widget->list_window->addWidget(&histogram[i], cr_histogram[i].top(), cr_histogram[i].left(), cr_histogram[i].height(), cr_histogram[i].width());

            curve[i].set_size_XY(start_y, end_y, step_grid_y,original_image.get_min_color(),original_image.get_max_color(), step_color);
            curve[i].set_title_coordinates(x_str,y_str[i]);
            curve[i].setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            widget->list_window->addWidget(&curve[i], cr_curve[i].top(), cr_curve[i].left(), cr_curve[i].height(), cr_curve[i].width());
        }
        break;
    }
    case ipd::tp_mono:
    {
        QString x_str = QString::fromUtf8("Histogram value");
        QString y_str = QString::fromUtf8("Brightness");
        histogram = new widget_histogram;
        histogram->set_size_XY(start_y, end_y, step_grid_y,original_image.get_min_color(),original_image.get_max_color(), step_color);
        histogram->set_title_coordinates(x_str, y_str);
        histogram->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        widget->list_window->addWidget(histogram, 0, 0, 1, 1);

        curve = new widget_curve;
        curve->set_size_XY(start_y, end_y, step_grid_y, original_image.get_min_color(),original_image.get_max_color(), step_color);
        curve->set_title_coordinates(x_str,y_str);
        curve->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        widget->list_window->addWidget(curve, 0, 1, 1, 1);
        break;
    }
    default:
    {
        throw ipd::ipd_exception(std::string("Image model is not supported (Supports RGB and monochrome image)"), std::string("ipd_img"));
    }
    }
}

void form_equalization::delete_params() //delete widget histogram and widget curve
{
    switch (original_image.get_color_type())
    {
    case ipd::tp_RGB:
    {
        if (histogram != NULL)
            delete []histogram;
        histogram = NULL;
        if (curve != NULL)
            delete []curve;
        curve = NULL;
        break;
    }
    case ipd::tp_mono:
    {
        if (histogram != NULL)
            delete histogram;
        histogram = NULL;
        if (curve != NULL)
            delete curve;
        curve = NULL;
        break;
    }
    default:
    {
        throw ipd::ipd_exception(std::string("Image model is not supported (Supports RGB and monochrome image)"), std::string("ipd_img"));
    }
    }
}

void form_equalization::show_img_settings() //show image settings
{
    create_params();
    int sz = equalization->get_count_points();
    double *h;
    double *y;
    double x, x1, x2;
    for (int i = 0; i < sz; i++)
    {
        equalization->get_XY(i, x, y);
        equalization->get_histogram(i, h, x1, x2);
        switch (convertible_image->get_color_type())
        {
        case ipd::tp_RGB:
        {
            int j;
            for (j = 0; j < sz_RGB; j++)
            {
                histogram[j].add_interval(x1, x2, *h++);
                curve[j].add_value(x, *y++);
            }
            if (i == sz - 1)
            {
                for (j = 0; j < sz_RGB; j++)
                {
                    histogram[j].set_data();
                    curve[j].set_data();
                }
            }
            break;
        }
        case ipd::tp_mono:
        {
            histogram->add_interval(x1, x2, *h);
            curve->add_value(x, *y);
            if (i == sz - 1)
            {
                histogram->set_data();
                curve->set_data();
            }
            break;
        }
        default:
        {
            throw ipd::ipd_exception(std::string("Image model is not supported (Supports RGB and monochrome image)"), std::string("ipd_img"));
        }

        }
    }
}